package com.zhangtai.modules.service.impl.promotion;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.PromotionBatchDao;
import com.zhangtai.modules.entity.promotion.PromotionBatchEntity;
import com.zhangtai.modules.service.promotion.PromotionBatchService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;


@RestController
public class PromotionBatchServiceImpl extends ServiceImpl<PromotionBatchDao, PromotionBatchEntity> implements PromotionBatchService {

    @Override
    public PageResult queryPage(@RequestParam(value = "batchCode", required = false) String batchCode,
                                @RequestParam(value = "userCode", required = false) String userCode,
                                @RequestParam(value = "type", required = false) Integer type,
                                @RequestParam(value = "number", required = false) Integer number,
                                @RequestParam(value = "startDate", required = false) String startDate,
                                @RequestParam(value = "endDate", required = false) String endDate,
                                @RequestParam(value = "orgIds", required = false) List<Long> orgIds,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<PromotionBatchEntity> page = this.page(
                new Page<PromotionBatchEntity>(pageNum,pageSize),
                new QueryWrapper<PromotionBatchEntity>().like(!StringUtils.isEmpty(batchCode),"promotion_code",batchCode)
                        .like(!StringUtils.isEmpty(userCode),"applyer_code",userCode)
                        .eq(!ObjectUtils.isEmpty(type),"type",type)
                        .between(!StringUtils.isEmpty(startDate)&&!StringUtils.isEmpty(endDate),"create_time",startDate,endDate)
                        .eq(CollectionUtils.isEmpty(orgIds),"unit_id",null)
                        .in(!CollectionUtils.isEmpty(orgIds),"unit_id",orgIds)
                        .eq("is_delete",0).orderByDesc("create_time")
        );

        return new PageResult(page);
    }



    @Override
    public PromotionBatchEntity getByIdE(Long id) {
            PromotionBatchEntity one = this.getOne(new QueryWrapper<PromotionBatchEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(PromotionBatchEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(PromotionBatchEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            PromotionBatchEntity entity = new PromotionBatchEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}